package lion.se.lucene;

import java.util.HashMap;
import java.util.Map;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper;
import org.apache.lucene.analysis.standard.StandardAnalyzer;

import lion.dev.lang.KV;

/**
 * @author Helin
 * @email hl_0074@sina.com
 * @date 2012-3-20 上午10:26:16
 * @des
 * 
 */
public class FieldAnalyzerMapper {

	public static Analyzer standard = new StandardAnalyzer();

	@SafeVarargs
	public static Analyzer getAnalyzer(KV<String, ? extends Analyzer>... analyzers) {

		Analyzer analyzer = standard;
		if (analyzers != null && analyzers.length > 0) {
			Map<String, Analyzer> fieldAnalyzers = new HashMap<String, Analyzer>();
			for (KV<String, ? extends Analyzer> kv : analyzers) {
				if (kv.getValue() == null) {
					continue;
				}
				fieldAnalyzers.put(kv.getKey(), kv.getValue());
			}
			analyzer = new PerFieldAnalyzerWrapper(standard, fieldAnalyzers);
		}

		return analyzer;
	}
}
